﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.Video;

public class VideoPreviewTool
{
    public static Texture GetPreviewTexture(System.Object previewID)
    {
        MethodInfo M = GetMethodInfo("GetPreviewTexture");
        Texture ret = (Texture)M.Invoke(null, new object[] { previewID });

        return ret;
    }

    public static System.Object Play(VideoClip clip)
    {
        System.Object previewID = null;

        MethodInfo M = GetMethodInfo("StartPreview");
        previewID = M.Invoke(null, new object[] { clip });

        MethodInfo playM = GetMethodInfo("PlayPreview");
        playM.Invoke(null, new object[] { previewID, true });

        return previewID;
    }

    public static void Stop(System.Object previewID)
    {
        MethodInfo M = GetMethodInfo("StopPreview");
        M.Invoke(null, new object[] { previewID });
    }

    public static void Pause(System.Object previewID)
    {
        MethodInfo M = GetMethodInfo("PausePreview");
        M.Invoke(null, new object[] { previewID });
    }

    public static MethodInfo GetMethodInfo(string name)
    {
        Type VideoUtil = Assembly.Load("UnityEditor.dll").GetType("UnityEditor.VideoUtil");
        MethodInfo ret = VideoUtil.GetMethod(name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
        return ret;
    }
}
